namespace QMVnSoft.SystemForms
{
    using QMVnSoft;
    using QMVnSoft.Controls;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;

    public class FormRestoreDatabase : FormControl
    {
        private BtnControl btnClose;
        private BtnControl btnOk;
        private BtnControl btnPathBackup;
        private IContainer components;
        private bool ib_CheckSucceed;
        private Label label3;
        private LabelControl labelControl1;
        private LabelControl labelControl2;
        private LabelControl lblBackup;
        private LabelControl lblPathBackup;
        private TextControl txtPathRestore;

        public FormRestoreDatabase()
        {
            this.InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.Do();
            base.Close();
        }

        private void btnPathRestore_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.InitialDirectory = "";
            dialog.Filter = "Backup file (*.bak)|*.bak";
            dialog.CheckFileExists = true;
            dialog.ShowDialog();
            this.txtPathRestore.Text = dialog.FileName;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void Do()
        {
            if (!string.IsNullOrEmpty(this.txtPathRestore.Text))
            {
                if (!File.Exists(this.txtPathRestore.Text))
                {
                    MessageBox.Show("Dữ liệu không tồn tại!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else if (!AppInstance.CheckServer())
                {
                    MessageBox.Show("Chức năng này chỉ thực hiện được tại máy chủ!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    string connectionString = "Server=" + AppInstance.ServerName + ";Database=master;User ID=" + AppInstance.ServerUserName + ";Password=" + AppInstance.ServerPassword + ";Connection Reset=FALSE;Min Pool Size=2;Connection Lifetime=120;Connection Timeout=60;";
                    string cmdText = "SELECT spid FROM sysprocesses ,sysdatabases WHERE sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='" + AppInstance.DatabaseName + "'";
                    SqlConnection connection = new SqlConnection(connectionString);
                    connection.Open();
                    SqlCommand command = new SqlCommand(cmdText, connection);
                    SqlDataReader reader = command.ExecuteReader();
                    ArrayList list = new ArrayList();
                    while (reader.Read())
                    {
                        list.Add(reader.GetInt16(0));
                    }
                    reader.Close();
                    for (int i = 0; i < list.Count; i++)
                    {
                        command = new SqlCommand(string.Format("KILL {0}", list[i]), connection);
                        command.ExecuteNonQuery();
                    }
                    command = new SqlCommand();
                    command.CommandType = CommandType.Text;
                    command.Connection = connection;
                    command.CommandText = "RESTORE DATABASE " + AppInstance.DatabaseName + " FROM DISK = '" + this.txtPathRestore.Text + "'";
                    try
                    {
                        command.ExecuteNonQuery();
                        this.CheckSucceed = true;
                        MessageBox.Show(AppInstance.EndMessage, AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message, AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
        }

        private void InitializeComponent()
        {
            //ComponentResourceManager manager = new ComponentResourceManager(typeof(FormRestoreDatabase));
            this.lblBackup = new LabelControl();
            this.btnPathBackup = new BtnControl();
            this.lblPathBackup = new LabelControl();
            this.txtPathRestore = new TextControl();
            this.btnOk = new BtnControl();
            this.btnClose = new BtnControl();
            this.labelControl1 = new LabelControl();
            this.labelControl2 = new LabelControl();
            this.label3 = new Label();
            base.SuspendLayout();
            this.lblBackup.AutoSize = true;
            this.lblBackup.BackColor = Color.White;
            this.lblBackup.Font = new Font("Arial", 8.25f);
            this.lblBackup.ForeColor = Color.Blue;
            this.lblBackup.Location = new Point(0x40, 0x27);
            this.lblBackup.Name = "lblBackup";
            this.lblBackup.Size = new Size(0xeb, 14);
            this.lblBackup.TabIndex = 0;
            this.lblBackup.Text = "Chức năng này chỉ thực hiện được tại máy chủ!";
            this.btnPathBackup.Font = new Font("Arial", 8.25f);
            this.btnPathBackup.Location = new Point(0x180, 0x74);
            this.btnPathBackup.Name = "btnPathBackup";
            this.btnPathBackup.Size = new Size(0x1d, 0x17);
            this.btnPathBackup.TabIndex = 1;
            this.btnPathBackup.TabStop = false;
            this.btnPathBackup.Text = "...";
            this.btnPathBackup.UseVisualStyleBackColor = true;
            this.btnPathBackup.Click += new EventHandler(this.btnPathRestore_Click);
            this.lblPathBackup.AutoSize = true;
            this.lblPathBackup.Font = new Font("Arial", 8.25f);
            this.lblPathBackup.Location = new Point(0x16, 120);
            this.lblPathBackup.Name = "lblPathBackup";
            this.lblPathBackup.Size = new Size(0x4e, 14);
            this.lblPathBackup.TabIndex = 1;
            this.lblPathBackup.Text = "Tên tệp dữ liệu";
            this.txtPathRestore.CheckTextSearched = false;
            this.txtPathRestore.DisabledForeColor = Color.Blue;
            this.txtPathRestore.Font = new Font("Arial", 8.25f);
            this.txtPathRestore.Location = new Point(0x6a, 0x75);
            this.txtPathRestore.Name = "txtPathRestore";
            this.txtPathRestore.OldValue = null;
            this.txtPathRestore.Size = new Size(0x110, 20);
            this.txtPathRestore.TabIndex = 0;
//            this.txtPathRestore.Value = null;//manager.GetObject("txtPathRestore.Value");
            this.btnOk.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.btnOk.Font = new Font("Arial", 8.25f);
            this.btnOk.Location = new Point(0xd3, 0xaf);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new Size(0x67, 0x17);
            this.btnOk.TabIndex = 2;
            this.btnOk.Text = "&Thực hiện";
            this.btnOk.UseVisualStyleBackColor = true;
            this.btnOk.Click += new EventHandler(this.btnOk_Click);
            this.btnClose.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            //this.btnClose.DialogResult = DialogResult.Cancel;
            this.btnClose.Font = new Font("Arial", 8.25f);
            this.btnClose.Location = new Point(320, 0xaf);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new Size(0x67, 0x17);
            this.btnClose.TabIndex = 3;
            this.btnClose.Text = "&Huỷ bỏ";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new EventHandler(this.btnClose_Click);
            this.labelControl1.BorderStyle = BorderStyle.Fixed3D;
            this.labelControl1.Font = new Font("Arial", 8.25f);
            this.labelControl1.Location = new Point(-1, 0x51);
            this.labelControl1.Name = "labelControl1";
            this.labelControl1.Size = new Size(0x1b5, 2);
            this.labelControl1.TabIndex = 3;
            this.labelControl2.BackColor = Color.White;
            this.labelControl2.Dock = DockStyle.Top;
            this.labelControl2.Font = new Font("Arial", 8.25f);
            this.labelControl2.Location = new Point(0, 0);
            this.labelControl2.Name = "labelControl2";
            this.labelControl2.Size = new Size(0x1b3, 0x51);
            this.labelControl2.TabIndex = 4;
            this.label3.AutoSize = true;
            this.label3.BackColor = Color.White;
            this.label3.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            this.label3.ForeColor = SystemColors.ActiveCaption;
            this.label3.Location = new Point(0x20, 0x16);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0xe5, 13);
            this.label3.TabIndex = 0x20;
            this.label3.Text = "Chọn tệp dữ liệu đã sao lưu để phục hồi!";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.CancelButton = this.btnClose;
            base.ClientSize = new Size(0x1b3, 210);
            base.Controls.Add(this.label3);
            base.Controls.Add(this.lblBackup);
            base.Controls.Add(this.labelControl2);
            base.Controls.Add(this.labelControl1);
            base.Controls.Add(this.btnPathBackup);
            base.Controls.Add(this.btnClose);
            base.Controls.Add(this.lblPathBackup);
            base.Controls.Add(this.btnOk);
            base.Controls.Add(this.txtPathRestore);
            //base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FormRestoreDatabase";
            this.Text = "Phục hồi dữ liệu";
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        public bool CheckSucceed
        {
            get
            {
                return this.ib_CheckSucceed;
            }
            set
            {
                this.ib_CheckSucceed = value;
            }
        }
    }
}

